package com.example.onlineeducation

import android.app.Application
import androidx.lifecycle.ViewModelProvider
import com.example.onlineeducation.data.constant.Constants
import com.example.onlineeducation.net.CookieIntercept
import com.example.onlineeducation.net.NetCode
import com.example.onlineeducation.ui.auth.LoginActivity
import com.example.onlineeducation.viewmodel.BaiduLocationVM
import com.example.onlineeducation.viewmodel.AppPushLogViewModel
import com.example.onlineeducation.viewmodel.AppViewModel
import com.shuyu.gsyvideoplayer.player.PlayerFactory
import com.shuyu.gsyvideoplayer.player.SystemPlayerManager
import com.tencent.bugly.crashreport.CrashReport
import com.yc.jetpacklib.init.YcJetpack
import me.jessyan.autosize.AutoSizeConfig

/**
 * Creator: yc
 * UseDes: * Date: 2021/6/24 17:1
 */
class App : Application() {

    companion object {
        lateinit var mInstant: App
            private set
    }

    val mViewModel by lazy {
        ViewModelProvider.AndroidViewModelFactory(this).create(AppViewModel::class.java)
    }

    val mPushLogViewModel by lazy {
        ViewModelProvider.AndroidViewModelFactory(this).create(AppPushLogViewModel::class.java)
    }
    val mBaiduLocationVM by lazy {
        ViewModelProvider.AndroidViewModelFactory(this).create(BaiduLocationVM::class.java)
    }

    override fun onCreate() {
        super.onCreate()
        mInstant = this

        //初始化Bugly设置
        val strategy = CrashReport.UserStrategy(this)
        strategy.isUploadProcess = !BuildConfig.DEBUG //是否上传错误日志0
        strategy.appVersion = BuildConfig.VERSION_NAME + "-" + BuildConfig.VERSION_CODE
        strategy.appPackageName = BuildConfig.APPLICATION_ID
        CrashReport.initCrashReport(this, "2ec3b39651", true, strategy)
        //视频播放初始化
        PlayerFactory.setPlayManager(SystemPlayerManager::class.java) //系统模式

        YcJetpack.mInstance.apply {
            init(this@App)
            mIsForceNoHandle = {
                when (it.code) {
                    NetCode.NET_CODE_TOKEN_INVALID -> {
                        LoginActivity.newInstanceTokenInvalid(this@App)
                        true
                    }
                    else -> false
                }
            }
            mImgIdResLoading = R.drawable.yc_loading
            mImgIdResFail = R.drawable.yc_loading_fail

            mDefaultBaseUrl = BuildConfig.base_url
            mNetSuccessCode = mutableListOf(NetCode.NET_CODE_SUCCESS)
            mInterceptor.add(CookieIntercept())
            AutoSizeConfig.getInstance().setPrivateFontScale(1f).isBaseOnWidth = true   //屏幕适配

        }
        mBaiduLocationVM.initLocation(this)
    }
}